﻿// (c) Copyright slimCODE Software Inc. - www.slimcode.com
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using SlimCode.Windows.Forms.Mobile;
using SlimCode.Windows.Controls.Mobile;

namespace SlimCode.SlimPasswords
{
  public partial class PasswordOptionsScreen : UserControl, IScreen
  {
    private IScreenHost m_host;

    public PasswordOptionsScreen()
    {
      InitializeComponent();
    }

    private void UpdateOptions()
    {
      optionsList.Items.Clear();

      optionsList.Items.Add( new PasswordLengthOptionView() );
      optionsList.Items.Add( new PasswordLowercasesOptionView() );
      optionsList.Items.Add( new PasswordUppercasesOptionView() );
      optionsList.Items.Add( new PasswordDigitsOptionView() );
      optionsList.Items.Add( new PasswordSymbolsOptionView() );
    }

    private void acceptMenuItem_Click( object sender, EventArgs e )
    {
      if( m_host != null )
      {
        m_host.HideScreen( DialogResult.OK );
      }
    }

    private void cancelMenuItem_Click( object sender, EventArgs e )
    {
      if( m_host != null )
      {
        m_host.HideScreen( DialogResult.Cancel );
      }
    }

    private void optionsList_ItemClick( object sender, ItemEventArgs args )
    {
      IOptionView optionView = args.Item as IOptionView;

      if( optionView != null )
      {
        optionView.OnClicked( m_host, this );
      }
    }

    private void AnyControl_GotFocus( object sender, EventArgs e )
    {
      if( m_host != null )
      {
        Program.ToggleInputPanelIf( m_host.MainForm.MainInputPanel, false );
      }
    }

    #region IScreen Members

    public  Control Control
    {
      get { return this; }
    }

    public Control DefaultFocus
    {
      get { return optionsList; }
    }

    public MainMenu Menu
    {
      get { return mainMenu; }
    }

    public string Title
    {
      get { return "slimPASSWORDS - Options"; }
    }

    public bool Hiding( DialogResult result )
    {
      if( result == DialogResult.OK )
      {
        foreach( ItemView itemView in optionsList.Items )
        {
          IOptionView optionView = itemView as IOptionView;

          if( optionView != null )
          {
            optionView.Apply();
          }
        }
      }
      else
      {
        foreach( ItemView itemView in optionsList.Items )
        {
          IOptionView optionView = itemView as IOptionView;

          if( optionView != null )
          {
            optionView.Cancel();
          }
        }
      }

      return true;
    }

    public void HidingChild( IScreen child, DialogResult result )
    {
    }

    public void Showing( IScreenHost host )
    {
      m_host = host;

      this.UpdateOptions();
    }

    #endregion
  }
}
